package com.jellyfishumbrella.GL;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.jellyfishumbrella.util.Trace;

public class PolygonGroup {// extends AbstractLOD {
	public String			name			= "undefined";
	private boolean			isDirty			= true;
	public MeshGL			parentMesh;

	public Set<Polygon>		polygons		= Collections.synchronizedSet(new HashSet<Polygon>());
	public List<Integer>	rawVertOrder	= Collections.synchronizedList(new ArrayList());

	public PolygonGroup() {
		this.parentMesh = null;
	}

	public PolygonGroup(MeshGL parent) {
		this.parentMesh = parent;
	}

	public synchronized void remove(UberPoly removeUberPoly) {
		synchronized (this.polygons) {
			Iterator<Polygon> itr = removeUberPoly.polygons.iterator();
			while (itr.hasNext()) {
				Polygon currPoly = itr.next();
				this.polygons.remove(currPoly);
			}
		}
	}

	public synchronized void remove(Polygon poly) {
		this.polygons.remove(poly);
	}

	public synchronized void clear() {
		synchronized (this.polygons) {
			this.rawVertOrder.clear();
			this.polygons.clear();
		}
	}

	public boolean isDirty() {
		return isDirty;
	}

	public void setDirty(boolean isDirty) {
		this.isDirty = isDirty;
	}

	public void rebuild() {
		Trace.log("------------REBUILD-------------" + this.name);
		this.rawVertOrder.clear();
		for (Polygon currPoly : this.polygons) {
			Trace.log(currPoly.toString());
			for (Vertex currVert : currPoly.verts) {
				Trace.log(currVert.getIndex() + " -- " + currVert.toString());
				if (currVert.getIndex() == 0) {
					System.out.println("GRRR :" + currVert.getIndex() + " -- " + currVert.toString());
				}
				this.rawVertOrder.add(currVert.getIndex());
			}
			this.rawVertOrder.add(Integer.MAX_VALUE);
			Trace.log("-------------------------");
		}
	}
}
